package com.situ.day24;

import org.junit.Test;

import java.io.*;
import java.util.Arrays;

public class IOTest {

    @Test
    public void testFileReader1() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("io.txt");
            int ch1 = fileReader.read();
            System.out.println(ch1);
            System.out.println((char)ch1);
            int ch2 = fileReader.read();
            System.out.println(ch2);
            System.out.println((char)ch2);
            int ch3 = fileReader.read();
            System.out.println(ch3);
            System.out.println((char)ch3);
            int ch4 = fileReader.read();
            System.out.println(ch4);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testFileReader2() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("io.txt");
            int ch = -1;
            while ((ch = fileReader.read()) != -1) {
                System.out.println(ch);
                System.out.println((char)ch);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void test22() {
        int num = 9;
        System.out.println(num = 12);
    }

    @Test
    public void testFileReader3() {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("io.txt");
            int length = -1;
            // public int read(char[] cbuf)
            // 将字符读入数组。在某个输入可用、发生 I/O 错误或者已到达流的末尾前，此方法一直阻塞。
            //  返回：读取的字符数，如果已到达流的末尾，则返回 -1
            char[] buffer = new char[5];
            // 12
            // 第一次  length: 5
            // 第二次  length: 5
            // 第三次  length: 2
            // 第四次  length: -1
            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(Arrays.toString(buffer));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testFileWriter() {
        FileReader fileReader = null;
        FileWriter fileWriter = null;
        try {
            fileReader = new FileReader("io.txt");
            fileWriter = new FileWriter("io_back.txt");
            char[] buffer = new char[5];
            int length = -1;
            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(Arrays.toString(buffer));
                fileWriter.write(buffer, 0, length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Test
    public void testInputOutputStream() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("bd.png");
            fileOutputStream = new FileOutputStream("bd_back.png");
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0 , length);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
